package cn.hbu.zlfw.exchange.controller;

import java.util.Date;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hbu.zlfw.admin.entity.Expert;
import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.DateSortUtil;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.dic.service.DicSchoolService;
import cn.hbu.zlfw.dic.service.DicUserTypeService;
import cn.hbu.zlfw.exchange.entity.Poster;
import cn.hbu.zlfw.exchange.entity.Replier;
import cn.hbu.zlfw.exchange.service.PosterService;
import cn.hbu.zlfw.patent.entity.Patent;
import cn.hbu.zlfw.patent.service.PatentService;
import cn.hbu.zlfw.research.entity.Researcher;

/**
 * 
 * @ClassName: PosterController 
 * @Description: 留言板主贴控制层
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年12月19日 下午9:48:52 
 * @vsersion:v1.0
 */
@RequestMapping("/poster")
@Controller
public class PosterController {
	/**
	 * 日志
	 */
	public final Logger logger=LoggerFactory.getLogger(PosterController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	/**
	 * 注入service
	 */
	@Autowired
	private PosterService posterService;
	@Autowired
	private DicUserTypeService dicUserTypeService;
	@Autowired
	private PatentService patentService;
	@Autowired
	private DicSchoolService dicSchoolService;
	
	
	/**
	 * 
	 * @Title: toPostlist 
	 * @Description: 转向专利所有交流问题列表
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.SELECT,description="查询专利所有交流问题")
	@RequestMapping({"/topostlist.action","/topostlist.do"})
	public String toPostlist(@ModelAttribute("pageModel")PageModel<Poster> pageModel,
			@RequestParam(value="patentId",required=false)String patentId,
			@RequestParam(value="title",required=false)String title,
			ModelMap map) {
		HttpSession session = request.getSession();
		if(patentId != null && patentId != "") {
			session.setAttribute("postSeesionPatentId", patentId);
		}
		int currentPage=pageModel.getCurrentPage();
		if(currentPage==0) {
			pageModel.setCurrentPage(1);
		}
		pageModel=this.posterService.listPostToPatent(pageModel,session.getAttribute("postSeesionPatentId").toString(),title);
		map.put("dicUserTypes", this.dicUserTypeService.findAll());
		map.put("dicSchools",this.dicSchoolService.findAll());
		map.put("patentName", this.patentService.getById( session.getAttribute("postSeesionPatentId").toString()).getName());
		map.put("patentId", session.getAttribute("postSeesionPatentId").toString());
		String url = request.getRequestURL().toString();
		if(url.contains(".do")) {
			pageModel.setUrl("/poster/topostlist.do");
			return "post/adminpostlist";
		} else  {
			pageModel.setUrl("/poster/topostlist.action");
			return "post/postlist";
		}
	}
	/**
	 * 
	 * @Title: topostQuestionUI 
	 * @Description: 转向提出问题的页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向提出问题的页面")
	@RequestMapping({"/postQuestionUI.action","/postQuestionUI.do"})
	public String postQuestionUI(@RequestParam(value="patentId",required=false)String patentId,ModelMap map) {
		map.put("patentId", patentId);
		String url = request.getRequestURL().toString();
		if(url.contains(".do")) {
			return "post/adminpostQuestion";
		} else  {
			return "post/postQuestion";
		}
	}
	/**
	 * 
	 * @Title: postQuestion 
	 * @Description: 存储提出的问题
	 * @return String 
	 * @throws
	 */
	@ResponseBody
	@OperatorLog(type=OperatorType.SAVE,description="存储提出的问题")
	@RequestMapping({"/postQuestion.action","/postQuestion.do"})
	public String postQuestion(@ModelAttribute("poster")Poster poster,
			@RequestParam(value="patentId",required=false)String patentId) {
		Patent patent = this.patentService.getById(patentId);
		Object obj = SessionUtil.getLoginUser(request);
		poster.setPatent(patent);
		poster.setUpdateTime(new Date());
		poster.setCreateTime(new Date());
		poster.setDelState(0);
		if(obj instanceof ScienceExpert) {
			ScienceExpert scienceExpert = (ScienceExpert)obj;
			poster.setUserName(scienceExpert.getUserName());
			poster.setUserType(scienceExpert.getUserType());
			poster.setDepartment(scienceExpert.getDepartment());
			poster.setSchoolType(scienceExpert.getSchoolType());
			this.posterService.save(poster);
			return "success";
		} else if (obj instanceof Researcher) {
			Researcher researcher = (Researcher)obj;
			poster.setUserName(researcher.getUserName());
			poster.setUserType(researcher.getUserType());
			poster.setDepartment(researcher.getDepartment());
			poster.setSchoolType(researcher.getSchoolType());
			this.posterService.save(poster);
			return "success";
		} else if (obj instanceof Expert) {
			Expert expert = (Expert)obj;
			poster.setUserName(expert.getUserName());
			poster.setUserType(expert.getUserType());
			poster.setDepartment(expert.getDepartment());
			poster.setSchoolType(expert.getSchoolType());
			this.posterService.save(poster);
			return "success";
		}  else {
			return "fail";
		}
	}
	
	/**
	 * @Title: topostContent 
	 * @Description: 转向问题讨论详细页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向问题讨论详细页面")
	@RequestMapping({"/postContentUI.action","/postContentUI.do"})
	public String postContentUI(@RequestParam(value="posterId",required=false)String posterId,ModelMap map) {
		Poster poster = this.posterService.getById(posterId);
		map.put("poster", poster);
		Set<Replier> repliers = poster.getRepliers();
		map.put("repliers", DateSortUtil.sortReplyByCTimeASC(repliers));
		map.put("dicUserTypes", this.dicUserTypeService.findAll());
		map.put("dicSchools",this.dicSchoolService.findAll());
		String url = request.getRequestURL().toString();
		if(url.contains(".do")) {
			return "post/adminpostContent";
		} else  {
			return "post/postContent";
		}
		
	}
}
